package cn.iocoder.yudao.module.acm.service.textileproduceplan;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.acm.controller.admin.textileproduceplan.vo.TextilePlanDetailRespVO;
import cn.iocoder.yudao.module.acm.controller.admin.textileproduceplan.vo.TextileProducePlanPageReqVO;
import cn.iocoder.yudao.module.acm.controller.admin.textileproduceplan.vo.TextileProducePlanRespVO;
import cn.iocoder.yudao.module.acm.controller.admin.textileproduceplan.vo.TextileProducePlanSaveReqVO;
import cn.iocoder.yudao.module.acm.dal.dataobject.textileplandetail.TextilePlanDetailDO;
import cn.iocoder.yudao.module.acm.dal.dataobject.textileproduceplan.TextileProducePlanDO;
import cn.iocoder.yudao.module.acm.dal.mysql.textileplandetail.TextilePlanDetailMapper;
import cn.iocoder.yudao.module.acm.dal.mysql.textileproduceplan.TextileProducePlanMapper;
import cn.iocoder.yudao.module.acm.enums.ACMCabinClassEnum;
import cn.iocoder.yudao.module.pplan.enums.PPlanProduceMarkStatusEnum;
import cn.iocoder.yudao.module.pplan.enums.PPlanProduceStatusEnum;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.acm.enums.ErrorCodeConstants.TEXTILE_PRODUCE_PLAN_NOT_EXISTS;

/**
 * 航机管理-纺织品生产计划 Service 实现类
 *
 * @author yisl
 */
@Service
@Validated
public class TextileProducePlanServiceImpl implements TextileProducePlanService {

    @Resource
    private TextileProducePlanMapper textileProducePlanMapper;
    @Resource
    private TextilePlanDetailMapper textilePlanDetailMapper;

    @Override
    public Long createTextileProducePlan(TextileProducePlanSaveReqVO createReqVO) {
        // 插入
        TextileProducePlanDO textileProducePlan = BeanUtils.toBean(createReqVO, TextileProducePlanDO.class);
        textileProducePlan.setMarkStatus(Integer.valueOf(PPlanProduceMarkStatusEnum.MARK_STATUS_1.getCode()));
        textileProducePlan.setStatus(Integer.valueOf(PPlanProduceStatusEnum.STATUS_ENUM_1.getCode()));
        textileProducePlan.setPlanCode("T"+System.currentTimeMillis());
        textileProducePlanMapper.insert(textileProducePlan);

        //插入纺织品详细
        List<TextilePlanDetailDO> detailDOS = new ArrayList<>();
        if (CollUtil.isNotEmpty(createReqVO.getFrontDetailList())){
            detailDOS.addAll(BeanUtils.toBean(createReqVO.getFrontDetailList(), TextilePlanDetailDO.class, clueVO -> {
                clueVO.setTextilePlanId(textileProducePlan.getId());
                clueVO.setCabainClass(Integer.valueOf(ACMCabinClassEnum.CABIN_CLASS_ENUM_1.getCode()));
            }));
        }
        if (CollUtil.isNotEmpty(createReqVO.getMiddleDetailList())){
            detailDOS.addAll(BeanUtils.toBean(createReqVO.getMiddleDetailList(), TextilePlanDetailDO.class, clueVO -> {
                clueVO.setTextilePlanId(textileProducePlan.getId());
                clueVO.setCabainClass(Integer.valueOf(ACMCabinClassEnum.CABIN_CLASS_ENUM_2.getCode()));
            }));
        }
        if (CollUtil.isNotEmpty(createReqVO.getRearDetailList())){
            detailDOS.addAll(BeanUtils.toBean(createReqVO.getRearDetailList(), TextilePlanDetailDO.class, clueVO -> {
                clueVO.setTextilePlanId(textileProducePlan.getId());
                clueVO.setCabainClass(Integer.valueOf(ACMCabinClassEnum.CABIN_CLASS_ENUM_3.getCode()));
            }));
        }
        textilePlanDetailMapper.insertBatch(detailDOS);

        // 返回
        return textileProducePlan.getId();
    }

    @Override
    public void updateTextileProducePlan(TextileProducePlanSaveReqVO updateReqVO) {
        // 校验存在
        validateTextileProducePlanExists(updateReqVO.getId());
        // 更新
        TextileProducePlanDO updateObj = BeanUtils.toBean(updateReqVO, TextileProducePlanDO.class);
        textileProducePlanMapper.updateById(updateObj);

        // 纺织品详情更新-先删除-再新增
        textilePlanDetailMapper.delete(TextilePlanDetailDO::getTextilePlanId, updateReqVO.getId());
        //插入机供品详细
        List<TextilePlanDetailDO> detailDOS = new ArrayList<>();
        if (CollUtil.isNotEmpty(updateReqVO.getFrontDetailList())){
            detailDOS.addAll(BeanUtils.toBean(updateReqVO.getFrontDetailList(), TextilePlanDetailDO.class, clueVO -> {
                clueVO.setTextilePlanId(updateReqVO.getId());
                clueVO.setCabainClass(Integer.valueOf(ACMCabinClassEnum.CABIN_CLASS_ENUM_1.getCode()));
            }));
        }
        if (CollUtil.isNotEmpty(updateReqVO.getMiddleDetailList())){
            detailDOS.addAll(BeanUtils.toBean(updateReqVO.getMiddleDetailList(), TextilePlanDetailDO.class, clueVO -> {
                clueVO.setTextilePlanId(updateReqVO.getId());
                clueVO.setCabainClass(Integer.valueOf(ACMCabinClassEnum.CABIN_CLASS_ENUM_2.getCode()));
            }));
        }
        if (CollUtil.isNotEmpty(updateReqVO.getRearDetailList())){
            detailDOS.addAll(BeanUtils.toBean(updateReqVO.getRearDetailList(), TextilePlanDetailDO.class, clueVO -> {
                clueVO.setTextilePlanId(updateReqVO.getId());
                clueVO.setCabainClass(Integer.valueOf(ACMCabinClassEnum.CABIN_CLASS_ENUM_3.getCode()));
            }));
        }
        textilePlanDetailMapper.insertBatch(detailDOS);
    }

    @Override
    public void deleteTextileProducePlan(Long id) {
        // 校验存在
        validateTextileProducePlanExists(id);
        // 删除
        textileProducePlanMapper.deleteById(id);
        textilePlanDetailMapper.delete(TextilePlanDetailDO::getTextilePlanId, id);
    }

    @Override
        public void deleteTextileProducePlanListByIds(List<Long> ids) {
        // 校验存在
        validateTextileProducePlanExists(ids);
        // 删除
        textileProducePlanMapper.deleteByIds(ids);
        }

    private void validateTextileProducePlanExists(List<Long> ids) {
        List<TextileProducePlanDO> list = textileProducePlanMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(TEXTILE_PRODUCE_PLAN_NOT_EXISTS);
        }
    }

    private void validateTextileProducePlanExists(Long id) {
        if (textileProducePlanMapper.selectById(id) == null) {
            throw exception(TEXTILE_PRODUCE_PLAN_NOT_EXISTS);
        }
    }

    @Override
    public TextileProducePlanRespVO getTextileProducePlan(Long id) {
        TextileProducePlanDO textileProducePlanDO = textileProducePlanMapper.selectById(id);
        TextileProducePlanRespVO textileProducePlanRespVO = BeanUtils.toBean(textileProducePlanDO, TextileProducePlanRespVO.class);

        List<TextilePlanDetailDO> detailDOS = textilePlanDetailMapper.selectList(TextilePlanDetailDO::getTextilePlanId, id);
        if (CollUtil.isNotEmpty(detailDOS)) {
            List<TextilePlanDetailRespVO> respVOS = BeanUtils.toBean(detailDOS, TextilePlanDetailRespVO.class);
            Map<String, List<TextilePlanDetailRespVO>> collect = respVOS.stream().collect(Collectors.groupingBy(TextilePlanDetailRespVO::getCabainClass));
            textileProducePlanRespVO.setFrontDetailList(collect.get(ACMCabinClassEnum.CABIN_CLASS_ENUM_1.getCode()));
            textileProducePlanRespVO.setMiddleDetailList(collect.get(ACMCabinClassEnum.CABIN_CLASS_ENUM_2.getCode()));
            textileProducePlanRespVO.setRearDetailList(collect.get(ACMCabinClassEnum.CABIN_CLASS_ENUM_3.getCode()));
        }
        return textileProducePlanRespVO;
    }

    @Override
    public PageResult<TextileProducePlanDO> getTextileProducePlanPage(TextileProducePlanPageReqVO pageReqVO) {
        return textileProducePlanMapper.selectPage(pageReqVO);
    }

}